package com.tarim.wheat.controller;

import com.tarim.wheat.model.Information;
import com.tarim.wheat.service.InformationService;
import com.tarim.wheat.util.Constants;
import com.tarim.wheat.util.JsonResult;
import com.tarim.wheat.vo.Criteria;
import com.tarim.wheat.vo.PageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.swing.*;

@CrossOrigin
@Controller
@RequestMapping("/information")
public class InformationController {
    @Autowired
    InformationService informationService;

    /**
     * 查询（分页）
     * @param pageIndex
     * @param pageSize
     * @param typeId
     * @param title
     * @return
     */
    @RequestMapping("/query")
    @ResponseBody
    public JsonResult query(@RequestParam(value = "pageIndex", defaultValue = "1") Integer pageIndex,
                            @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                            @RequestParam(value = "typeId", defaultValue = "") Long typeId,
                            @RequestParam(value = "title", defaultValue = "") String title){
        JsonResult js;
        try {
            PageBean pageBean = new PageBean(pageIndex, pageSize);
            Criteria criteria = new Criteria();
            criteria.addExtField("typeId", typeId);
            criteria.addExtField("title", title);
            pageBean = informationService.selectByCondition(criteria, pageBean);
            js = new JsonResult(Constants.STATUS_SUCCESS, "成功", pageBean);
        }catch (Exception ex){
            js = new JsonResult(Constants.STATUS_ERROR, "异常");
        }
        return js;
    }

    /**
     * 获取文章详情
     * @param id
     * @return
     */
    @RequestMapping(value = "/getDetails")
    @ResponseBody
    public JsonResult getDetails(@RequestParam(value = "id") Long id){
        JsonResult js;
        try {
            if(id == null){
                return new JsonResult(Constants.STATUS_FAIL, "请选择文章");
            }
            Information information = informationService.selectByPrimaryKey(id);
            if(information == null){
                js = new JsonResult(Constants.STATUS_NOT_FOUND, "获取文章详情失败");
            }else {
                js = new JsonResult(Constants.STATUS_SUCCESS, "获取文章详情成功", information);
            }
        }catch (Exception ex){
            ex.printStackTrace();
            js = new JsonResult(Constants.STATUS_ERROR, "获取文章详情异常");
        }
        return js;
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ResponseBody
    public JsonResult updateForDelete(Long id){
        JsonResult js;
        try {
            int i = informationService.updateForDelete(id);
            if(i == 0){
                js = new JsonResult(Constants.STATUS_FAIL, "操作失败");
            }else {
                js = new JsonResult(Constants.STATUS_SUCCESS, "操作成功");
            }
        }catch (Exception e){
            e.printStackTrace();
            js = new JsonResult(Constants.STATUS_ERROR, "操作异常");
        }
        return js;
    }

    /**
     * 发布文章
     * @param information
     * @return
     */
    @RequestMapping(value = "/insert", method = RequestMethod.POST)
    @ResponseBody
    public JsonResult insert(Information information){
        JsonResult js;
        try {
            int i = informationService.insert(information);
            if(i == 0){
                js = new JsonResult(Constants.STATUS_FAIL, "发布失败");
            }else {
                js = new JsonResult(Constants.STATUS_SUCCESS, "发布成功");
            }
        }catch (Exception e){
            e.printStackTrace();
            js = new JsonResult(Constants.STATUS_ERROR, "发布异常");
        }
        return js;
    }
}
